﻿using System;
using System.Xml;
using System.Reflection;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;
using System.Globalization;
using CaveDigital.BoardMeeting2007Lite.ServerLib;
using CaveDigital.SharedLib;
using System.Diagnostics;
using Microsoft.SharePoint.WebPartPages;
using System.Collections.Specialized;
using System.IO;
using System.Web;
using System.Text;

namespace CaveDigital.BoardMeeting2007Lite.CaveDigitalCSPDeploymentPackage 
{
    class CaveDigitalCSPDeploymentPackage_handler : SPFeatureReceiver
    {

		#region Const Fields (7) 

        private const string CONTENTTYPE_DOCUMENT_TEMPLATE = "template.docx";
        private const string EVENT_ASSEMBLY = "CaveDigital.BoardMeeting2007Lite.MeetingSiteCreator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2651e18e216567e9";
        private const string EVENT_ASSEMBLY_CLASS = "MeetingSiteCreator.MeetingSiteCreator";
        private const string EVENT_PRODUCTNAME = "CaveDigital.BoardMeeting2007Lite";
        private const string FEATURE_GUID_MINUTES = "3fad61c5-2048-46fb-9f56-54eee77f191b";
        private const string FEATURE_GUID_WORKFLOW = "bf0b9ea0-7855-472f-8f82-b32605878ab7";
        private const string FIELD_TITLE = "Title";

		#endregion Const Fields 

		#region Fields (2) 
        private List<SPGroup> groupsList = new List<SPGroup>();
		#endregion Fields 

		#region Overridden Methods (4) 

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb myCatchWeb = null;
            SPSite caveDigitalMeetingsPortal = null;
            SPWeb caveDigitalWebPortal = null;
            if (Convert.ToString(properties.Feature.DefinitionId) == FEATURE_GUID_MINUTES
                || Convert.ToString(properties.Feature.DefinitionId) == FEATURE_GUID_WORKFLOW)
            {
                try
                {
                    // Get Portal Contet
                    if (properties.Feature.Parent != null && properties.Feature.Parent is SPSite)
                    {
                        caveDigitalMeetingsPortal = properties.Feature.Parent as SPSite;
                        caveDigitalWebPortal = caveDigitalMeetingsPortal.OpenWeb();
                    }
                    else
                    {
                        caveDigitalWebPortal = properties.Feature.Parent as SPWeb;
                    }
                    caveDigitalWebPortal.AllowUnsafeUpdates = true;
                    myCatchWeb = caveDigitalWebPortal;
                    //Get Parent Culture
                    int lcid = caveDigitalWebPortal.CurrencyLocaleID;
                    CultureInfo specificCulture = new CultureInfo(lcid);
                    //Get Site Defenition
                    SPWebTemplateCollection Templates = caveDigitalWebPortal.GetAvailableWebTemplates(Convert.ToUInt32(specificCulture.LCID));
                    SPWebTemplate siteTemplate = Templates["STS#1"];
                    caveDigitalWebPortal.Webs.Add(CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetCollectionObjectsString(Localization.RESOURCES_CAPTION_COUNCILMEETINGS_URL, specificCulture), CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetCollectionObjectsString(Localization.RESOURCES_CAPTION_COUNCILMEETINGS_KEY, specificCulture), "", Convert.ToUInt32(specificCulture.LCID), siteTemplate, true, false);
                    caveDigitalWebPortal.Update();
                    SPWeb myNewPortalWeb = caveDigitalWebPortal.Webs[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetCollectionObjectsString(Localization.RESOURCES_CAPTION_COUNCILMEETINGS_URL, specificCulture)];
                    //Set Master Page
                    myNewPortalWeb.MasterUrl = myNewPortalWeb.ParentWeb.MasterUrl;
                    myNewPortalWeb.CustomMasterUrl = myNewPortalWeb.ParentWeb.CustomMasterUrl;
                    myNewPortalWeb.AlternateCssUrl = myNewPortalWeb.ParentWeb.AlternateCssUrl;
                    myNewPortalWeb.Update();

                    myNewPortalWeb.AllowUnsafeUpdates = true;
                    myNewPortalWeb.RoleDefinitions.BreakInheritance(true, true);
                    myNewPortalWeb.BreakRoleInheritance(false);
                    myNewPortalWeb.AllowUnsafeUpdates = true;
                    myNewPortalWeb.Update();
                    // Set Lists
                    myNewPortalWeb.Lists.Add(CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetMeetingsPortalString(MeetingsPortalObjects.MeetingMinutesDocLib, specificCulture), "", SPListTemplateType.DocumentLibrary);
                    myNewPortalWeb.Lists.Add(CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetMeetingsPortalString(MeetingsPortalObjects.CalendarList, specificCulture), "", SPListTemplateType.GenericList);
                    //Add Groups
                    SPGroupCollection myGroupColl = myNewPortalWeb.SiteGroups;
                    ValidateGroups(caveDigitalWebPortal);
                    SPGroup Participants = myGroupColl[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.Participants, specificCulture)];
                    int ParticipantsID = Participants.ID;
                    groupsList.Add(Participants);
                    Participants.OnlyAllowMembersViewMembership = false;
                    Participants.Update();
                    SPGroup LegalAdvisers = myGroupColl[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.LegalAdvisers, specificCulture)];
                    int LegalAdvisersID = LegalAdvisers.ID;
                    groupsList.Add(LegalAdvisers);
                    LegalAdvisers.OnlyAllowMembersViewMembership = false;
                    LegalAdvisers.Update();
                    SPGroup Secretaries = myGroupColl[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.Secretaries, specificCulture)];
                    int SecretariesID = Secretaries.ID;
                    groupsList.Add(Secretaries);
                    Secretaries.OnlyAllowMembersViewMembership = false;
                    Secretaries.Update();
                    SPGroup Mayors = myGroupColl[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.Mayors, specificCulture)];
                    int MayorsID = Mayors.ID;
                    groupsList.Add(Mayors);
                    Mayors.OnlyAllowMembersViewMembership = false;
                    Mayors.Update();
                    SPGroup Approvers = myGroupColl[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.Approvers, specificCulture)];
                    int ApproversID = Approvers.ID;
                    groupsList.Add(Approvers);
                    Approvers.OnlyAllowMembersViewMembership = false;
                    Approvers.Update();
                    //Add Lists
                    SPList myDocLibMinutes = myNewPortalWeb.Lists[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetMeetingsPortalString(MeetingsPortalObjects.MeetingMinutesDocLib, specificCulture)];
                    SPList myCalendarList = myNewPortalWeb.Lists[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetMeetingsPortalString(MeetingsPortalObjects.CalendarList, specificCulture)];
                    myDocLibMinutes.BreakRoleInheritance(true);
                    myCalendarList.BreakRoleInheritance(true);
                    myDocLibMinutes.Update();
                    myCalendarList.Update();
                    //Add fields to Calendar List
                    myCalendarList.Fields.Add(CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_LOCATION_KEY, specificCulture), SPFieldType.Text, false);
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_LOCATION_KEY, specificCulture)].Title = CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_LOCATION_KEY, specificCulture);
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_LOCATION_KEY, specificCulture)].ShowInDisplayForm = true;
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_LOCATION_KEY, specificCulture)].ShowInEditForm = true;
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_LOCATION_KEY, specificCulture)].ShowInNewForm = true;
                    myCalendarList.Fields.Add(CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_DESCRIPTION_KEY, specificCulture), SPFieldType.Note, false);
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_DESCRIPTION_KEY, specificCulture)].Title = CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_DESCRIPTION_KEY, specificCulture);
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_DESCRIPTION_KEY, specificCulture)].ShowInDisplayForm = true;
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_DESCRIPTION_KEY, specificCulture)].ShowInEditForm = true;
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_DESCRIPTION_KEY, specificCulture)].ShowInNewForm = true;
                    myCalendarList.Fields.Add(CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_STARTDATETIME_KEY), SPFieldType.DateTime, true);
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_STARTDATETIME_KEY)].Title = CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_STARTDATETIME_KEY);
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_STARTDATETIME_KEY)].ShowInDisplayForm = true;
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_STARTDATETIME_KEY)].ShowInEditForm = true;
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_STARTDATETIME_KEY)].ShowInNewForm = true;
                    myCalendarList.Fields.Add(CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_ENDDATETIME_KEY), SPFieldType.DateTime, true);
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_ENDDATETIME_KEY)].Title = CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_ENDDATETIME_KEY);
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_ENDDATETIME_KEY)].ShowInDisplayForm = true;
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_ENDDATETIME_KEY)].ShowInEditForm = true;
                    myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_ENDDATETIME_KEY)].ShowInNewForm = true;
                    SPFieldCollection myCollFiel = myCalendarList.Fields;
                    SPFieldUser participantField = new SPFieldUser(myCollFiel, CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.Participants), CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.Participants));
                    participantField.AllowMultipleValues = true;
                    participantField.Presence = true;
                    participantField.ShowInDisplayForm = true;
                    participantField.ShowInEditForm = true;
                    participantField.ShowInNewForm = true;
                    participantField.Required = true;
                    participantField.SelectionGroup = ParticipantsID;
                    participantField.SelectionMode = SPFieldUserSelectionMode.PeopleOnly;
                    myCalendarList.Fields.Add(participantField);
                    SPFieldUser SecretaryField = new SPFieldUser(myCollFiel, Wss30.EscapeSpecialCharacters(CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.Secretary)), CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.Secretary));
                    SecretaryField.AllowMultipleValues = true;
                    SecretaryField.Presence = true;
                    SecretaryField.ShowInDisplayForm = true;
                    SecretaryField.ShowInEditForm = true;
                    SecretaryField.ShowInNewForm = true;
                    SecretaryField.Required = true;
                    SecretaryField.SelectionGroup = SecretariesID;
                    SecretaryField.SelectionMode = SPFieldUserSelectionMode.PeopleOnly;
                    myCalendarList.Fields.Add(SecretaryField);
                    SPFieldUser legalAdviserField = new SPFieldUser(myCollFiel, Wss30.EscapeSpecialCharacters(CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.LegalAdviser)), CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.LegalAdviser));
                    legalAdviserField.AllowMultipleValues = true;
                    legalAdviserField.Presence = true;
                    legalAdviserField.ShowInDisplayForm = true;
                    legalAdviserField.ShowInEditForm = true;
                    legalAdviserField.ShowInNewForm = true;
                    legalAdviserField.Required = true;
                    legalAdviserField.SelectionGroup = LegalAdvisersID;
                    legalAdviserField.SelectionMode = SPFieldUserSelectionMode.PeopleOnly;
                    myCalendarList.Fields.Add(legalAdviserField);
                    myCalendarList.Fields.Add(CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_QRYINAME_MEETINGSPORTAL_SITE_NAME, specificCulture), SPFieldType.Text, false);
                    SPField meetingSiteField = myCalendarList.Fields[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_QRYINAME_MEETINGSPORTAL_SITE_NAME, specificCulture)];
                    meetingSiteField.Update();
                    meetingSiteField.ShowInDisplayForm = true;
                    meetingSiteField.ShowInEditForm = false;
                    meetingSiteField.ShowInNewForm = false;
                    meetingSiteField.Update();
                    //Register Event Handlers 
                    myCalendarList.EventReceivers.Add(SPEventReceiverType.ItemAdded, EVENT_ASSEMBLY, EVENT_ASSEMBLY_CLASS);
                    myCalendarList.EventReceivers.Add(SPEventReceiverType.ItemAdding, EVENT_ASSEMBLY, EVENT_ASSEMBLY_CLASS);
                    myCalendarList.EventReceivers.Add(SPEventReceiverType.ItemDeleted, EVENT_ASSEMBLY, EVENT_ASSEMBLY_CLASS);
                    myCalendarList.EventReceivers.Add(SPEventReceiverType.ItemDeleting, EVENT_ASSEMBLY, EVENT_ASSEMBLY_CLASS);
                    myCalendarList.EventReceivers.Add(SPEventReceiverType.ItemUpdated, EVENT_ASSEMBLY, EVENT_ASSEMBLY_CLASS);
                    myCalendarList.EventReceivers.Add(SPEventReceiverType.ItemUpdating, EVENT_ASSEMBLY, EVENT_ASSEMBLY_CLASS);
                    myCalendarList.Update();
                    SPRoleDefinitionCollection myColl = myNewPortalWeb.RoleDefinitions;
                    SPRoleDefinition approveRole = myColl[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.Contribute, specificCulture)];
                    int approveId = approveRole.Id;
                    foreach (SPGroup group in groupsList)
                    {
                        SPRoleDefinition myRole = myNewPortalWeb.RoleDefinitions.GetById(approveId);
                        AddPermissionsGroups(myRole, group, myNewPortalWeb, myCalendarList, myDocLibMinutes);
                    }
                    //Obtain parent ContentType
                    SPContentType parentContentType = GetDocumentContentType(myNewPortalWeb);
                    SPContentTypeCollection myDocLibContentTypes = myDocLibMinutes.ContentTypes;
                    SPContentTypeCollection myContentTypeColl = myNewPortalWeb.ContentTypes;
                    SPContentType newContentType = new SPContentType(parentContentType, myContentTypeColl, CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetCollectionObjectsString(Localization.RESOURCES_CAPTION_COUNCILMEETINGS_MINUTES_TEMPLATE_KEY));
                    SPContentType newWebCtype = myNewPortalWeb.ContentTypes.Add(newContentType);
                    SPContentType newDocLibCtype = myDocLibMinutes.ContentTypes.Add(newWebCtype);
                    SPFolder templateFolder = GetTemplateFolder(myNewPortalWeb, myDocLibMinutes.Title);
                    //Remove inherited templates
                    for (int i = 0; i < templateFolder.Files.Count; ++i)
                        templateFolder.Files[i].Delete();
                    FileStream fStream = File.OpenRead(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), string.Concat(@"Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\Minute_3fad61c5-2048-46fb-9f56-54eee77f191b\", CONTENTTYPE_DOCUMENT_TEMPLATE)));
                    SPFile minutesTemplate = templateFolder.Files.Add(CONTENTTYPE_DOCUMENT_TEMPLATE, fStream);
                    //Add the template to the ContentType                    
                    fStream.Close();
                    newDocLibCtype.DocumentTemplate = CONTENTTYPE_DOCUMENT_TEMPLATE;
                    newDocLibCtype.Update();
                    //Remove other inhereted ContentTypes
                    for (int i = 0; i < myDocLibMinutes.ContentTypes.Count; ++i)
                        if (myDocLibMinutes.ContentTypes[i].Name != newDocLibCtype.Name)
                            myDocLibMinutes.ContentTypes[i].Delete();
                    SPViewCollection views = myCalendarList.Views;
                    StringCollection strColFields = new StringCollection();
                    strColFields.AddRange(new string[] { 
                        CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_STARTDATETIME_KEY), 
                        CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_ENDDATETIME_KEY), 
                        FIELD_TITLE, 
                        Wss30.EscapeSpecialCharacters(CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.Participants)), 
                        Wss30.EscapeSpecialCharacters(CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.Secretary)), 
                        Wss30.EscapeSpecialCharacters(CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.LegalAdviser)) 
                    });
                    string viewQuery = string.Format("<Where><DateRangesOverlap><FieldRef Name=\"{0}\" /><FieldRef Name=\"{1}\" /><Value Type=\"DateTime\"><Month /></Value></DateRangesOverlap></Where>",
                        CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_STARTDATETIME_KEY),
                        CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetListColumn(MinutesConstants.RESOURCES_INAME_MEETINGSPORTAL_ENDDATETIME_KEY));
                    SPView newView = views.Add(CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetMeetingsPortalString(MeetingsPortalObjects.CalendarList, specificCulture), strColFields, viewQuery, 15, false, true, SPViewCollection.SPViewType.Calendar, false);
                    newView.Update();
                    SPFile defaultPagePortal = myNewPortalWeb.GetFile("default.aspx");
                    if (defaultPagePortal != null)
                    {
                        //get the web part manager 
                        SPLimitedWebPartManager webPartMgr = defaultPagePortal.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
                        SPList calendarList = myNewPortalWeb.Lists[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetMeetingsPortalString(MeetingsPortalObjects.CalendarList, specificCulture)];
                        ListViewWebPart calendarWebPart = new ListViewWebPart();
                        calendarWebPart.Title = CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetMeetingsPortalString(MeetingsPortalObjects.CalendarList);
                        calendarWebPart.ListName =
                        calendarList.ID.ToString("B").ToUpper();
                        calendarWebPart.Hidden = false;
                        calendarWebPart.ViewType = ViewType.Calendar;
                        calendarWebPart.ViewGuid = calendarList.DefaultView.ID.ToString("B").ToUpper();
                        webPartMgr.AddWebPart(calendarWebPart, "Left", 0);
                    }
                    System.Diagnostics.EventLog.WriteEntry(EVENT_PRODUCTNAME, "The CaveDigital Board Meeting 2007 Lite, was successfully installed and Meetings Portal is ready for use.", System.Diagnostics.EventLogEntryType.SuccessAudit);
                    myNewPortalWeb.Dispose();
                    caveDigitalWebPortal.Dispose();
                    caveDigitalMeetingsPortal.Dispose();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.EventLog.WriteEntry(EVENT_PRODUCTNAME, "The CaveDigital Board Meeting 2007 Lite, was not successfully installed in your server." + ex.Message, System.Diagnostics.EventLogEntryType.Error);
                }
                finally
                {
                    if (caveDigitalWebPortal != null)
                        caveDigitalWebPortal.Dispose();
                    if (caveDigitalMeetingsPortal != null)
                        caveDigitalMeetingsPortal.Dispose();
                }
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            SPSite caveDigitalMeetingsPortal = (SPSite)properties.Feature.Parent;
            SPWeb caveDigitalWebPortal = caveDigitalMeetingsPortal.OpenWeb();
            try
            {
                Guid feature1Guid1 = new Guid(FEATURE_GUID_MINUTES);
                Guid feature1Guid2 = new Guid(FEATURE_GUID_WORKFLOW);
                FeatureActivate(caveDigitalWebPortal, feature1Guid1);
                FeatureActivate(caveDigitalWebPortal, feature1Guid2);
            }
            catch (Exception) { }
            finally 
            {
                if(caveDigitalWebPortal!=null)
                    caveDigitalWebPortal.Dispose();
                if(caveDigitalMeetingsPortal!=null)
                    caveDigitalMeetingsPortal.Dispose();
            }
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            SPSite caveDigitalMeetingsPortal = (SPSite)properties.Feature.Parent;
            SPWeb caveDigitalWebPortal = caveDigitalMeetingsPortal.OpenWeb();
            try
            {
                Guid feature1Guid1 = new Guid(FEATURE_GUID_MINUTES);
                Guid feature1Guid2 = new Guid(FEATURE_GUID_WORKFLOW);
                FeatureDeactivate(caveDigitalWebPortal, feature1Guid1);
                FeatureDeactivate(caveDigitalWebPortal, feature1Guid2);
            }
            catch (Exception) { }
            finally
            {
                if (caveDigitalWebPortal != null)
                    caveDigitalWebPortal.Dispose();
                if (caveDigitalMeetingsPortal != null)
                    caveDigitalMeetingsPortal.Dispose();
            }
        }

		#endregion Overridden Methods 

		#region Private Methods (8) 

        private void AddPermissionsGroups(SPRoleDefinition roleDefenition, SPGroup group, SPWeb portalWeb, SPList calendarList, SPList minutesLib)
        {
            SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
            roleAssignment.RoleDefinitionBindings.Add(roleDefenition);
            portalWeb.RoleAssignments.Add(roleAssignment);
            calendarList.RoleAssignments.Add(roleAssignment);
            minutesLib.RoleAssignments.Add(roleAssignment);
        }

        private void CheckGroups(SPWeb web, params string[] groupNames)
        {
            for (int i = 0; i < groupNames.Length ; ++i)
                if (!GroupExists(web, groupNames[i]))
                    web.SiteGroups.Add(groupNames[i], web.CurrentUser, web.CurrentUser, "");
        }

        private bool FeatureActivate(SPWeb myWeb, Guid featureGuid)
        {
            try
            {
                myWeb.Features.Add(featureGuid, true);
                System.Diagnostics.EventLog.WriteEntry(EVENT_PRODUCTNAME, 
                    string.Format("The FEATURE with ID:'{0}' was successfully installed!",featureGuid.ToString()), System.Diagnostics.EventLogEntryType.SuccessAudit);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry(EVENT_PRODUCTNAME, 
                    string.Format("The FEATURE with ID:'{0}' was not installed. {1}",featureGuid.ToString(), ex.Message), System.Diagnostics.EventLogEntryType.Error);
                return false;
            }
            return true;
        }

        private bool FeatureDeactivate(SPWeb myWeb, Guid featureGuid)
        {
            try
            {
                myWeb.Features.Remove(featureGuid, true);
                System.Diagnostics.EventLog.WriteEntry(EVENT_PRODUCTNAME,
                    string.Format("The FEATURE with ID:'{0}' was successfully uninstalled!", featureGuid.ToString()), System.Diagnostics.EventLogEntryType.SuccessAudit);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry(EVENT_PRODUCTNAME,
                    string.Format("The FEATURE with ID:'{0}' was not uninstalled. {1}", featureGuid.ToString(), ex.Message), System.Diagnostics.EventLogEntryType.Error);
                return false;
            }
            return true;
        }

        private SPContentType GetDocumentContentType(SPWeb web)
        {
            SPContentTypeId cTypeId = new SPContentTypeId("0x0101");
            return web.AvailableContentTypes[cTypeId];
        }

        private SPFolder GetTemplateFolder(SPWeb web, string listName)
        {
            string docUrl = null;
            SPList list = null;
            SPContentType cType = null;
            if (web != null)
            {
                list = web.Lists[listName];
                if (list == null)
                    return null;
                cType = list.ContentTypes[CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetCollectionObjectsString(Localization.RESOURCES_CAPTION_COUNCILMEETINGS_MINUTES_TEMPLATE_KEY)];
                if (cType == null)
                    return null;
                docUrl = cType.DocumentTemplateUrl;
                return web.GetFolder(docUrl.Substring(0, docUrl.IndexOf(cType.DocumentTemplate)));
            }
            return null;
        }

        private bool GroupExists(SPWeb web, string groupName)
        {
            SPGroupCollection groups = web.SiteGroups;
            for (int i = 0; i < groups.Count; ++i)
                if (groups[i].Name == groupName)
                {
                    groups[i].OnlyAllowMembersViewMembership = false;
                    groups[i].Update();
                    return true;
                }
            return false;
        }

        private void ValidateGroups(SPWeb web)
        {
            CheckGroups(web, CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.Mayors), CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.Participants), CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.LegalAdvisers), CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.Secretaries), CaveDigital.BoardMeeting2007Lite.ServerLib.Localization.GetRoleString(Roles.Approvers));
        }

		#endregion Private Methods 

    }
}